library(survey)
library(foreign)
library(ggplot2)
library(cowplot)
library(dplyr)
library(egg)
library(ggpubr)
library(gridGraphics)

## see wave1_CLEANING.R for data cleaning
load("~/Dropbox/Public_Conf_Mil/Data_and_code/wave1_data/clean_dataw1.RData")

## Create weighted survey design object
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

addline_format <- function(x,...){
  gsub('\\s','\n',x)
}

#### TREATMENT EFFECTS FOR CONFIDENCE IN MILITARY ####

treat_means <- svyby(~ Q8_d, ~ DOV_ASSIGNMENT_A, w1_design, svymean, na.rm = TRUE)

treat_df <- data.frame(conf = treat_means[,2] * 100,
                       ci_lo = (treat_means[,2] - 1.96*treat_means[,3]) * 100,
                       ci_hi = (treat_means[,2] + 1.96*treat_means[,3]) * 100,
                       cond = c(1:8))

treat_plot <- ggplot(treat_df) +
  geom_hline(yintercept = treat_df$conf[1], colour = "red", lty = 1) +
  geom_pointrange(aes(x = cond, y = conf, ymin = ci_lo, ymax = ci_hi), 
                  fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = conf, label = sprintf("%0.2f", round(conf, digits = 2))),
            size = 3, vjust = 2.5) +
  xlab("Treatment Condition") +
  scale_x_continuous(breaks = c(1:8),
                     labels = addline_format(c("Control", 
                                               "Republican Military",
                                               "Democrat Military",
                                               "Polarized Military",
                                               "Lost Afghanistan War",
                                               "Won Afghanistan War",
                                               "Ethical Generals",
                                               "Unethical Generals"))) +
  ylab("Respondents with Confidence in Military (%)") +
  theme_bw()
treat_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave1_treateffects.jpeg", 
       plot = treat_plot, dpi = 1000)

#### TREATMENT EFFECTS BY CONFIDENCE BY PARTY ####

treat_means2 <- svyby(~ Q8_d, ~ DOV_ASSIGNMENT_A+party, w1_design, svymean, na.rm = TRUE)

treat_df2 <- data.frame(dem = treat_means2[1:8,3]*100,
                        dem_lo = (treat_means2[1:8,3] - 1.96*treat_means2[1:8,4]) * 100,
                        dem_hi = (treat_means2[1:8,3] + 1.96*treat_means2[1:8,4]) * 100,
                        ind = treat_means2[9:16,3]*100,
                        ind_lo = (treat_means2[9:16,3] - 1.96*treat_means2[9:16,4]) * 100,
                        ind_hi = (treat_means2[9:16,3] + 1.96*treat_means2[9:16,4]) * 100,
                        rep = treat_means2[17:24,3]*100,
                        rep_lo = (treat_means2[17:24,3] - 1.96*treat_means2[17:24,4]) * 100,
                        rep_hi = (treat_means2[17:24,3] + 1.96*treat_means2[17:24,4]) * 100,
                        dcond = c(1,5,9,13,17,21,25,29),
                        icond = c(2,6,10,14,18,22,26,30),
                        rcond = c(3,7,11,15,19,23,27,31))

treat_plot2 <- ggplot(treat_df2) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Respondents with Confidence in the Military (%)") +
  scale_x_continuous(breaks = c(2,6,10,14,18,22,26,30),
                     labels = addline_format(c("Control", 
                                               "Republican Military",
                                               "Democrat Military",
                                               "Polarized Military",
                                               "Lost Afghanistan War",
                                               "Won Afghanistan War",
                                               "Ethical Generals",
                                               "Unethical Generals"))) +
  theme_bw()
treat_plot2
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave1_treatbyparty.jpeg", 
       plot = treat_plot2, dpi = 1000)

#### TREATMENT EFFECTS FOR BORDER DEPLOYMENT BY PARTY ####

fig71_means <- svyby(~Q42A_d, ~DOV_ASSIGNMENT_C+party, w1_design, svymean, 
                     na.rm = TRUE)[c(2:6,8:12,14:18),]

fig71_df <- data.frame(dem = fig71_means[1:5,3] * 100,
                       dem_lo = (fig71_means[1:5,3] - 1.96*fig71_means[1:5,4]) * 100,
                       dem_hi = (fig71_means[1:5,3] + 1.96*fig71_means[1:5,4]) * 100,
                       ind = fig71_means[6:10,3] * 100,
                       ind_lo = (fig71_means[6:10,3] - 1.96*fig71_means[6:10,4]) * 100,
                       ind_hi = (fig71_means[6:10,3] + 1.96*fig71_means[6:10,4]) * 100,
                       rep = fig71_means[11:15,3] * 100,
                       rep_lo = (fig71_means[11:15,3] - 1.96*fig71_means[11:15,4]) * 100,
                       rep_hi = (fig71_means[11:15,3] + 1.96*fig71_means[11:15,4]) * 100,
                       dcond = c(1,5,9,13,17),
                       icond = c(2,6,10,14,18),
                       rcond = c(3,7,11,15,19))

fig71_plot <- ggplot(fig71_df) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Support for Border Deployment (%)") +
  #geom_hline(yintercept = 0, colour = "black", lty = 1) +
  scale_x_continuous(breaks = c(2,6,10,14,18),
                     labels = addline_format(c("Control", 
                                               "Trump Politicizes",
                                               "Democrat Politicizes",
                                               "Military Supports",
                                               "Military Opposes"))) +
  theme_bw()
fig71_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave1_border_party.jpeg", 
       plot = fig71_plot, dpi = 1000)

#### TRUST BY BORDER DEPLOYMENT AND PARTY ####

fig72_means <- svyby(~Q42F_d, ~DOV_ASSIGNMENT_C+party, w1_design, svymean, 
                     na.rm = TRUE)[c(2:6,8:12,14:18),]

fig72_df <- data.frame(dem = fig72_means[1:5,3] * 100,
                       dem_lo = (fig72_means[1:5,3] - 1.96*fig72_means[1:5,4]) * 100,
                       dem_hi = (fig72_means[1:5,3] + 1.96*fig72_means[1:5,4]) * 100,
                       ind = fig72_means[6:10,3] * 100,
                       ind_lo = (fig72_means[6:10,3] - 1.96*fig72_means[6:10,4]) * 100,
                       ind_hi = (fig72_means[6:10,3] + 1.96*fig72_means[6:10,4]) * 100,
                       rep = fig72_means[11:15,3] * 100,
                       rep_lo = (fig72_means[11:15,3] - 1.96*fig72_means[11:15,4]) * 100,
                       rep_hi = (fig72_means[11:15,3] + 1.96*fig72_means[11:15,4]) * 100,
                       dcond = c(1,5,9,13,17),
                       icond = c(2,6,10,14,18),
                       rcond = c(3,7,11,15,19))

fig72_plot <- ggplot(fig72_df) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Trust in the Military (%)") +
  #geom_hline(yintercept = 0, colour = "black", lty = 1) +
  scale_x_continuous(breaks = c(2,6,10,14,18),
                     labels = addline_format(c("Control", 
                                               "Trump Politicizes",
                                               "Democrat Politicizes",
                                               "Military Supports",
                                               "Military Opposes"))) +
  theme_bw()
fig72_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave1_border_trust.jpeg", 
       plot = fig72_plot, dpi = 1000)
